/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package be.ugent.medirem.logic;

import be.ugent.medirem.persistence.entity.details.MedicationDetails;
import be.ugent.medirem.persistence.entity.details.UnitDetails;
import be.ugent.medirem.persistence.entity.values.MedicationValues;
import be.ugent.medirem.persistence.exception.EntityNotFoundException;
import be.ugent.medirem.persistence.exception.UnitNotFoundException;
import be.ugent.medirem.persistence.facade.MedicationDaoLocal;
import be.ugent.medirem.persistence.facade.UnitDaoLocal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Thomas Roelens <thomas.roelens@ugent.be>
 */
@Stateless
public class MedicationBean implements MedicationRemote {
    @EJB
    private UnitDaoLocal unitDaoBean;
    @EJB
    private MedicationDaoLocal medicationDaoBean;

    @RolesAllowed("gp")
    public Long createMedication(MedicationValues details) throws IllegalArgumentException, EntityNotFoundException {
        return medicationDaoBean.create(details);
    }

    @RolesAllowed("gp")
    public void removeMedication(Long medicationId) throws IllegalArgumentException {
        try {
            medicationDaoBean.remove(medicationId);
        } catch (EntityNotFoundException ex) {
            Logger.getLogger(MedicationBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @RolesAllowed("gp")
    public void updateMedication(Long medicationId, MedicationValues details) throws IllegalArgumentException, EntityNotFoundException {
        medicationDaoBean.update(medicationId, details);
    }

    @RolesAllowed({"gp", "patient"})
    public MedicationDetails getMedicationDetails(Long medicationId) throws IllegalArgumentException, EntityNotFoundException {
        return medicationDaoBean.getDetails(medicationId);
    }

    @RolesAllowed("gp")
    public List<MedicationDetails> searchMedication(String medicationName) {
        return medicationDaoBean.search(medicationName);
    }

    @RolesAllowed("gp")
    public List<MedicationDetails> getAllMedications() {
        return medicationDaoBean.getAllMedications();
    }

    @RolesAllowed("gp")
    public Long createUnit(String name) throws IllegalArgumentException {
        return unitDaoBean.create(name);
    }

    @RolesAllowed("gp")
    public void removeUnit(Long id) throws IllegalArgumentException, UnitNotFoundException {
        unitDaoBean.remove(id);
    }

    @RolesAllowed("gp")
    public UnitDetails getUnit(Long id) throws IllegalArgumentException {
        return unitDaoBean.getUnit(id);
    }

    @RolesAllowed("gp")
    public List<UnitDetails> getAllUnits() {
        return unitDaoBean.getAll();
    }

 
}
